﻿using BCGM.MedicalWisdom.IServices;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.Models.Patients;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace BCGM.MedicalWisdom.Api.Controllers
{
    /// <summary>
    /// 后台患者管理——诊断记录
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class diagnoseController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IdiagnoseServices _diagnoseServices;
        private readonly IDepartmentServices _departmentServices;

        public diagnoseController(IdiagnoseServices diagnoseServices, IDepartmentServices departmentServices)
        {
            _diagnoseServices = diagnoseServices;
            _departmentServices = departmentServices;
        }
        /// <summary>
        /// 诊断记录显示列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<diagnose>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<diagnose, bool>> whereExpression = a => true;

            return new MessageModel<PageModel<diagnose>>()
            {
                msg = "获取成功",
                success = true,
                response = await _diagnoseServices.QueryPage(whereExpression, page, intPageSize)
            };

        }
        /// <summary>
        /// 根据Id查询诊断记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<diagnose>> Get(string id)
        {
            return new MessageModel<diagnose>()
            {
                msg = "获取成功",
                success = true,
                response = await _diagnoseServices.QueryById(id)
            };
        }
        /// <summary>
        /// 添加诊断记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] diagnose request)
        {
            var data = new MessageModel<string>();

            var id = await _diagnoseServices.Add(request);
            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }
        /// <summary>
        /// 修改诊断记录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] diagnose request)
        {
            var data = new MessageModel<string>();
            data.success = await _diagnoseServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.DiagnoseID.ObjToString();
            }

            return data;
        }
        /// <summary>
        /// 删除诊断记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            var model = await _diagnoseServices.QueryById(id);
            model.IsDeleted = true;
            data.success = await _departmentServices.Update(model);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = model?.DiagnoseID.ObjToString();
            }

            return data;
        }
    }
}